Introduce the simpleBuffer as implementation of the httputil.BufferPool. #44
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
There are some bugs in the buffer pool implementation of the infra package.
authorization-proxy/infra/buffer.go
Line 74 in 4bc56bb
The copyBuffer function in the httputil.ReverseProxy package checks the length of the slice that given as argument(buf), and if the length of the slice is 0, it makes a slice as 32 * 1024 (32KB).
https://github.com/golang/go/blob/69234ded30614a471c35cef5d87b0e0d3c136cd9/src/net/http/httputil/reverseproxy.go#L642
The authorisation-proxy (infra package) always reset the slice length to 0 before put it to the pool, this means that the buffer size is always 32KB when copying.
authorization-proxy/infra/buffer.go
Line 64 in 4bc56bb
Even if a slice with the same capacity as buffer.size is put, a new slice will be created.
Since httputil.ReverseProxy does not change the slice size, results in memory allocation every time a response is copied.
The impact of this issue is limited, you probably can't notice it if you don't specify a large value for bufferSize (see result of memory usage).
In this PR, a new simpleBuffer is introduced to solve the above problem.
The current implementation of BufferPool is overkill because the httputil.Reverseproxy does not resize the slice used to copy the response.
However the current implementation is still exists, it could be used for other purposes.
e.g. memory usage (origin server returns small response)
before
after
Type of change
Flags
Related issue/PR
Delete this section if there are no issues or pull requests that relate to this pull request.
Checklist
[skip ci]
/[ci skip]
/[no ci]
/[skip actions]
/[actions skip]
in the PR title if necessaryChecklist for maintainer
Squash and merge
[skip ci]
/[ci skip]
/[no ci]
/[skip actions]
/[actions skip]